package com.zrar.ak.gkpt.service.impl.record;

import com.zrar.ak.gkpt.dao.*;
import com.zrar.ak.gkpt.model.bo.*;
import com.zrar.ak.gkpt.model.vo.SchemeContainerVO;
import com.zrar.ak.gkpt.model.vo.SchemeUpdateContainerVo;
import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.record.IBusinessSchemeInfoService;
import com.zrar.ak.gkpt.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SchemeInformationService
 * @Description: ISchemeInformationService接口实现类
 * @Author: CaoTao
 * @Create: 2020/04/04
 */
@Service
public class BusinessSchemeInfoService implements IBusinessSchemeInfoService {

    @Autowired
    private IBusinessSchemeTerminalInfoDao terminalDao;
    @Autowired
    private IBusinessSchemeSystemInfoDao systemInfoDao;
    @Autowired
    private IBusinessSchemePeripheralInfoDao peripheralInfoDao;
    @Autowired
    private IBusinessSchemeServerInfoDao serverInfoDao;
    @Autowired
    private IBusinessSchemeSoftwareHardwareInfoDao softwareHardwareInfoDao;
    @Autowired
    private IBusinessSchemeBasicsDao basicsDao;

    /**
     * 保存方案细化基本信息
     *
     * @param jsonResult
     * @param formDataJson
     * @param userVO
     * @return
     */
    @Override
    public JsonResult schemeBasicInfoSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        try {
            BusinessSchemeBasicsBO businessSchemeBasicsBO = CommonTools.jsonStrToObject(formDataJson, BusinessSchemeBasicsBO.class);
            businessSchemeBasicsBO.setCreatorId(userVO.getUserId());
            businessSchemeBasicsBO.setCreateTime(new Date());
            businessSchemeBasicsBO.setUpdateUserId(userVO.getUserId());
            businessSchemeBasicsBO.setUpdateTime(new Date());
            businessSchemeBasicsBO.setId(CommonTools.getUUID());
            businessSchemeBasicsBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
            String unitId = businessSchemeBasicsBO.getUnitId();
            // 存在建设单位就不允许添加
            int count = basicsDao.selectByUnitId(unitId);
            if (count > 0) {
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_EXISTENT_DATA);
            } else {
                basicsDao.insert(businessSchemeBasicsBO);
                // 返回基础表中的主键id,并将结果返回
                BusinessSchemeBasicsBO basicsBO = basicsDao.selectOne(businessSchemeBasicsBO);
                String basicsId = basicsBO.getId();
                Map map = new HashMap();
                map.put("basicsId", basicsId);
                jsonResult.setData(map);
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
            }

        } catch (IOException e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    /**
     * 方案细化详细信息保存
     *
     * @param jsonResult
     * @param formDataJson
     * @param userVO
     * @return JsonResult
     */
    @Override
    public JsonResult schemeInfoSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        Map map = new HashMap();
        int queryCount = 0;
        SchemeContainerVO schemeContainer = new SchemeContainerVO();
        // 将json数据转换为SchemeContainerVO封装类
        try {
            schemeContainer = CommonTools.jsonStrToObject(formDataJson, SchemeContainerVO.class);
            //判断插入的数据是否存在
            List<BusinessSchemePeripheralInfoBO> peripheralList = schemeContainer.getPeripheral();
            List<BusinessSchemeServerInfoBO> serverList = schemeContainer.getServer();
            List<BusinessSchemeSoftwareHardwareInfoBO> softList = schemeContainer.getSoft();
            List<BusinessSchemeSystemInfoBO> systemList = schemeContainer.getSystem();
            List<BusinessSchemeTerminalInfoBO> terminalList = schemeContainer.getTerminal();
            // 记录查询条数
            // 判断外设是否已经存在数据
            for (BusinessSchemePeripheralInfoBO per : peripheralList) {
                // 终端类型
                String peripheralTerminalType = per.getPeripheralTerminalType();
                // 外设类型
                String peripheralType = per.getPeripheralType();
                // 存入外设表主键
                per.setId(CommonTools.getUUID());
                // 基础信息主键
                String basicsId = per.getBasicsId();
                map.put("basicsId", basicsId);
                map.put("peripheralTerminalType", peripheralTerminalType);
                map.put("peripheralType", peripheralType);
                // 根据终端类型、外设类型和基础信息主键查询，有数据不允许插入
                queryCount = peripheralInfoDao.queryByType(map) + queryCount;
            }
            // 判断服务是否已经存在数据
            for (BusinessSchemeServerInfoBO server : serverList) {
                // 服务器端类型
                String serverType = server.getServerType();
                // 部署位置
                String serverDeploymentLocation = server.getServerDeploymentLocation();
                // 应用系统名称
                String serverSystemName = server.getServerSystemName();
                // 存入服务表主键
                server.setId(CommonTools.getUUID());
                // 基础信息主键
                String basicsId = server.getBasicsId();
                map.put("basicsId", basicsId);
                map.put("serverType", serverType);
                map.put("serverDeploymentLocation", serverDeploymentLocation);
                map.put("serverSystemName", serverSystemName);
                // 有数据不允许插入
                queryCount = serverInfoDao.queryByType(map) + queryCount;
            }
            // 判断软硬件信息是否已经存在
            for (BusinessSchemeSoftwareHardwareInfoBO soft : softList) {
                // 软硬件类型
                String softType = soft.getSoftType();
                // 终端/服务器类型位置
                String terminalServerType = soft.getTerminalServerType();
                // 存入硬件表主键
                soft.setId(CommonTools.getUUID());
                // 基础信息主键
                String basicsId = soft.getBasicsId();
                map.put("basicsId", basicsId);
                map.put("softType", softType);
                map.put("terminalServerType", terminalServerType);
                // 有数据不允许插入
                queryCount = softwareHardwareInfoDao.queryByType(map) + queryCount;
            }
            // 判断系统信息是否已经存在
            for (BusinessSchemeSystemInfoBO system : systemList) {
                // 应该系统名称
                String systemName = system.getSystemName();
                // 模式
                String model = system.getModel();
                // 是否上云
                String isCloud = system.getIsCloud();
                // 功能调整
                String functionAdjust = system.getFunctionAdjust();
                // 部署位置
                String systemDeploymentLocation = system.getSystemDeploymentLocation();
                // 存入系统表主键
                system.setId(CommonTools.getUUID());
                // 基础信息主键
                String basicsId = system.getBasicsId();
                map.put("basicsId", basicsId);
                map.put("systemName", systemName);
                map.put("model", model);
                map.put("isCloud", isCloud);
                map.put("functionAdjust", functionAdjust);
                map.put("systemDeploymentLocation", systemDeploymentLocation);
                // 有数据不允许插入
                queryCount = systemInfoDao.queryByType(map) + queryCount;
            }
            // 判断终端信息是否已经存在
            for (BusinessSchemeTerminalInfoBO terminal : terminalList) {
                // 应该系统名称
                String terminalSystemName = terminal.getTerminalSystemName();
                // 终端类型
                String terminalType = terminal.getTerminalType();
                // 部署位置
                String terminalDeploymentLocation = terminal.getTerminalDeploymentLocation();
                // 存入终端表主键
                terminal.setId(CommonTools.getUUID());
                // 获取终端表中的基本信息表主键
                String basicsId = terminal.getBasicsId();
                map.put("basicsId", basicsId);
                map.put("terminalSystemName", terminalSystemName);
                map.put("terminalType", terminalType);
                map.put("terminalDeploymentLocation", terminalDeploymentLocation);
                // 有数据不允许插入
                queryCount = terminalDao.queryByType(map)+ queryCount;
            }
            if (queryCount > 0) {
                // 数据已存在，返会信息
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_EXISTENT_DATA);
            } else {
                terminalDao.insertInfo(terminalList);
                systemInfoDao.insertInfo(systemList);
                peripheralInfoDao.insertInfo(peripheralList);
                serverInfoDao.insertInfo(serverList);
                softwareHardwareInfoDao.insertInfo(softList);
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    /**
     * 根据基础信息表中主键id删除方案信息
     *
     * @param basicsId 基础信息主键id
     * @return JsonResult
     */
    @Override
    public JsonResult deleteSchemeByBasicsId(JsonResult jsonResult, String basicsId) {
        /*int terminal = terminalDao.deleteTerminalById(basicsId);
        int system = systemInfoDao.deleteSystemlById(basicsId);
        int server = serverInfoDao.deleteServerlById(basicsId);
        int peripheral = peripheralInfoDao.deletePeripheralById(basicsId);*/

        BusinessSchemeBasicsBO basicsBO = new BusinessSchemeBasicsBO();
        basicsBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        basicsBO.setId(basicsId);
        basicsDao.updateByPrimaryKey(basicsBO);
        jsonResult = CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    /**
     * 根据主键id查看方案信息
     *
     * @param basicsId 基础信息主键
     * @return JsonResult
     */
    @Override
    public JsonResult querySchemeByUnitId(JsonResult jsonResult, String basicsId) {

        SchemeContainerVO schemeContainerVO = new SchemeContainerVO();
        // 查询数据
        List<BusinessSchemeTerminalInfoBO> terminalList = terminalDao.queryAllByBasicsId(basicsId);
        List<BusinessSchemeSystemInfoBO> systemList = systemInfoDao.queryAllByBasicsId(basicsId);
        List<BusinessSchemePeripheralInfoBO> peripherList = peripheralInfoDao.queryAllByBasicsId(basicsId);
        List<BusinessSchemeSoftwareHardwareInfoBO> softList = softwareHardwareInfoDao.queryAllByBasicsId(basicsId);
        List<BusinessSchemeServerInfoBO> serverList = serverInfoDao.queryAllByBasicsId(basicsId);
        // 封装数据
        schemeContainerVO.setPeripheral(peripherList);
        schemeContainerVO.setServer(serverList);
        schemeContainerVO.setSystem(systemList);
        schemeContainerVO.setTerminal(terminalList);
        schemeContainerVO.setSoft(softList);

        jsonResult.setData(schemeContainerVO);
        CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        return jsonResult;

    }

    @Override
    public JsonPageResult getSchemeInfoList(JsonPageResult jsonResult, int currentPage, int pageSize, String unitName) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        if (StringUtils.isEmpty(unitName)) {
            unitName = null;
        }
        Map map = new HashMap();
        map.put("currentPage", currentPage);
        map.put("pageSize", pageSize);
        map.put("unitName", unitName);
        // 封装查询数据
        List<BusinessSchemeInfoListBO> list = null;
        //符合展示条件的数据总数
        Integer total = null;
        //total = terminalDao.querySchemeShowNum(map);
        //list = terminalDao.pagingSchemeInfoList(map);

        total = basicsDao.querySchemeShowNum(map);
        list = basicsDao.pagingSchemeInfoList(map);
        if (total == null) {
            total = 0;
        }
        //计算一共多少页
        Integer pageNum = (total % pageSize == 0) ? (total / pageSize) : (total / pageSize + 1);

        jsonResult.setData(list);
        jsonResult.setCurrentPage(currentPage);
        jsonResult.setPageSize(pageSize);
        jsonResult.setTotalCount(total);
        jsonResult.setTotalPage(pageNum);
        if (list == null || list.size() == 0) {
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_NONEXISTENT_DATA);
        } else {
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        }
        return jsonResult;
    }

    /**
     * 更新
     *
     * @param jsonResult
     * @param formDataJson
     * @param userVO
     * @return
     */
    @Override
    public JsonResult schemeTerminalInfoUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {

        Map map = null;
        SchemeContainerVO schemeContainer = new SchemeContainerVO();
        SchemeUpdateContainerVo schemeUpdateContainer = new SchemeUpdateContainerVo();
        // 将json数据转换为SchemeContainerVO封装类
        try {
            schemeUpdateContainer = CommonTools.jsonStrToObject(formDataJson, SchemeUpdateContainerVo.class);
            String unitId = schemeUpdateContainer.getUnitId();
            BusinessSchemeBasicsBO businessSchemeBasicsBO = basicsDao.queryStatusByUnitId(unitId);
            String basicsId = businessSchemeBasicsBO.getId();
            List<BusinessSchemePeripheralInfoBO> peripheralList = schemeUpdateContainer.getSchemeContainer().getPeripheral();
            List<BusinessSchemeServerInfoBO> serverList = schemeUpdateContainer.getSchemeContainer().getServer();
            List<BusinessSchemeSoftwareHardwareInfoBO> softList = schemeUpdateContainer.getSchemeContainer().getSoft();
            List<BusinessSchemeSystemInfoBO> systemList = schemeUpdateContainer.getSchemeContainer().getSystem();
            List<BusinessSchemeTerminalInfoBO> terminalList = schemeUpdateContainer.getSchemeContainer().getTerminal();
            // 判断接收的数据中基本数据主键和传入建设单位的主键是否相同
            int count = 0;
            for (BusinessSchemePeripheralInfoBO per : peripheralList) {
                if(basicsId == per.getBasicsId()){
                    count++;
                }
            }
            for (BusinessSchemeServerInfoBO server : serverList) {
                if(basicsId == server.getBasicsId()){
                    count++;
                }
            }
            for (BusinessSchemeSoftwareHardwareInfoBO soft : softList) {
                if(basicsId == soft.getBasicsId()){
                    count++;
                }
            }
            for (BusinessSchemeSystemInfoBO system : systemList) {
                if(basicsId == system.getBasicsId()){
                    count++;
                }
            }
            for (BusinessSchemeTerminalInfoBO terminalInfo : terminalList) {
                if(basicsId == terminalInfo.getBasicsId()){
                    count++;
                }
            }
            if(businessSchemeBasicsBO == null || BusinessEnum.IS_DELETE_Y.equals(businessSchemeBasicsBO.getIsDelete())){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
            }else if(count > 0){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            }else{
                terminalDao.updateInfo(terminalList);
                systemInfoDao.updateInfo(systemList);
                peripheralInfoDao.updateInfo(peripheralList);
                serverInfoDao.updateInfo(serverList);
                softwareHardwareInfoDao.updateInfo(softList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }

        /*Map map = new HashMap();
        String updateUserId = userVO.getUserId();
        Date updateTime = new Date();
        try {
            // 将json数据转换为map
            map = CommonTools.jsonStrToObject(formDataJson, Map.class);
            //判断更新的数据是否存在
            int peripheralCount = peripheralInfoDao.queryPeripherByUnitId(map);
            int terminalCount = terminalDao.queryTerminalByUnitId(map);
            int systemCount = systemInfoDao.querySystemByUnitId(map);
            int serverCount = serverInfoDao.queryServerByUnitId(map);
            int softCount = softwareHardwareInfoDao.querySoftByUnitId(map);
            if (peripheralCount <= 0 || terminalCount <= 0 || systemCount <= 0 || serverCount <= 0 || softCount <= 0) {
                // 数据不存在，返会信息，不允许更新
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_NONEXISTENT_DATA);
            } else {
                String cteatorId = userVO.getUserId();
                Date createTime = new Date();
                map.put("createTime", createTime);
                map.put("creatorId", cteatorId);
                map.put("updateTime", updateTime);
                map.put("updateUserId", updateUserId);
                map.put("isDelete", BusinessEnum.IS_DELETE_N.getValue());

                // 判断是否更新成功
                int terminal = terminalDao.updateInfo(map);
                int system = systemInfoDao.updateInfo(map);
                int peripheral = peripheralInfoDao.updateInfo(map);
                int server = serverInfoDao.updateInfo(map);
                int soft = softwareHardwareInfoDao.updateInfo(map);
                if (terminal > 0 && system > 0 && peripheral > 0 && server > 0 && soft > 0) {
                    CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }*/
        return jsonResult;
    }


}
